package com.shyroke.daydayzhuanweb.controller;

import com.alibaba.druid.util.StringUtils;
import com.shyroke.daydayzhuanapi.dto.SubjectAddRequest;
import com.shyroke.daydayzhuanapi.dto.SubjectAdminDto;
import com.shyroke.daydayzhuanapi.dto.SubjectAdminRequest;
import com.shyroke.daydayzhuanapi.dto.UserMain;
import com.shyroke.daydayzhuanapi.entity.SubjectAppEntity;
import com.shyroke.daydayzhuanapi.entity.SubjectEntity;
import com.shyroke.daydayzhuandao.respository.SubjectAppRepository;
import com.shyroke.daydayzhuandao.respository.SubjectRepository;
import com.shyroke.daydayzhuansvc.service.impl.OSSService;
import com.shyroke.daydayzhuansvc.service.impl.SubjectService;
import com.shyroke.daydayzhuansvc.util.Page;
import com.shyroke.daydayzhuansvc.util.R;
import com.shyroke.daydayzhuansvc.util.TabelData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "admin/subject")
public class AdminSubjectController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SubjectService subjectService;
    @Autowired
    private SubjectRepository subjectRepository;
    @Autowired
    private SubjectAppRepository subjectAppRepository;
    @Autowired
    private OSSService ossService;


    @GetMapping("listView")
    public ModelAndView listView(ModelAndView model, HttpSession session){
        model.setViewName("admin/subject/list");
        return model;
    }

    @GetMapping("list")
    @ResponseBody
    public TabelData<SubjectAdminDto> list(Page page, SubjectAdminRequest request){
        return subjectService.page(page,request);
    }

    @GetMapping("addView")
    public ModelAndView addView(ModelAndView model){
        model.setViewName("admin/subject/add");
        return model;
    }

    @PostMapping("save")
    @ResponseBody
    public R save(@RequestBody SubjectAddRequest request,HttpSession session){
        try {
            UserMain user = (UserMain)session.getAttribute("user");
            subjectService.save(request,user.getId());
        }catch (Exception e){
            logger.error("保存专题异常：",e);
            return R.error("保存失败！");
        }
        return R.ok("保存成功");
    }

    @PostMapping("offline")
    @ResponseBody
    public R offline(@RequestParam("id")Integer id,@RequestParam("isShow")Boolean isShow){
        try {
            subjectService.offline(id,isShow);
        }catch (Exception e){
            logger.error("上下架专题异常：",e);
            return R.error("上下架专题失败！");
        }
        return R.ok("操作成功");
    }

    @PostMapping("commented")
    @ResponseBody
    public R commented(@RequestParam("id")Integer id,@RequestParam("isCommented")Boolean isCommented){
        try {
            subjectService.commented(id,isCommented);
        }catch (Exception e){
            logger.error("推荐专题异常：",e);
            return R.error("推荐专题失败！");
        }
        return R.ok("操作成功");
    }

    @GetMapping("editView/{id}")
    public ModelAndView addView(ModelAndView model,@PathVariable("id")Integer subjectId){
        Optional<SubjectEntity> optionalSubject = subjectRepository.findById(subjectId);
        if (optionalSubject.isPresent()) {
            SubjectEntity subjectEntity = optionalSubject.get();
            List<SubjectAppEntity> subjectAppList = subjectAppRepository.findBySubjectId(subjectEntity.getId());
            List<Integer> appIdList = subjectAppList.stream().map(sa -> sa.getAppId()).collect(Collectors.toList());
            if(!StringUtils.isEmpty(subjectEntity.getCover())){
                subjectEntity.setCover(ossService.getImgUrl(subjectEntity.getCover()));
            }
            model.addObject("subject",subjectEntity);
            model.addObject("appIdList",appIdList);
        }
        model.setViewName("admin/subject/update");
        return model;
    }

    @PostMapping("update")
    @ResponseBody
    public R update(@RequestBody SubjectAddRequest request){
        try {
            subjectService.update(request);
        }catch (Exception e){
            logger.error("修改专题异常：",e);
            return R.error("保存失败！");
        }
        return R.ok("修改成功");
    }


    @GetMapping("all")
    @ResponseBody
    public List<SubjectAdminDto> all(){
        return subjectService.all();
    }

}
